﻿using System;
using UnityEngine;

// Token: 0x020002E2 RID: 738
public class ThirdPersonOrbitCam : MonoBehaviour
{
	// Token: 0x06001117 RID: 4375 RVA: 0x0007E400 File Offset: 0x0007C600
	private void Awake()
	{
		this.cam = base.transform;
		this.playerControl = this.player.GetComponent<Player>();
		this.relCameraPos = base.transform.position - this.player.position;
		this.relCameraPosMag = this.relCameraPos.magnitude - 0.5f;
		this.smoothPivotOffset = this.pivotOffset;
		this.smoothCamOffset = this.camOffset;
		this.defaultFOV = this.cam.GetComponent<Camera>().fieldOfView;
	}

	// Token: 0x06001118 RID: 4376 RVA: 0x0007E490 File Offset: 0x0007C690
	private void LateUpdate()
	{
		this.angleH += Mathf.Clamp(Input.GetAxis("Mouse X"), -1f, 1f) * this.horizontalAimingSpeed * Time.deltaTime;
		this.angleV += Mathf.Clamp(Input.GetAxis("Mouse Y"), -1f, 1f) * this.verticalAimingSpeed * Time.deltaTime;
		if (this.playerControl.IsFlying())
		{
			this.angleV = Mathf.Clamp(this.angleV, this.minVerticalAngle, this.flyMaxVerticalAngle);
		}
		else
		{
			this.angleV = Mathf.Clamp(this.angleV, this.minVerticalAngle, this.maxVerticalAngle);
		}
		Quaternion rotation = Quaternion.Euler(-this.angleV, this.angleH, 0f);
		Quaternion rotation2 = Quaternion.Euler(0f, this.angleH, 0f);
		this.cam.rotation = rotation;
		if (this.playerControl.IsAiming())
		{
			this.targetPivotOffset = this.aimPivotOffset;
			this.targetCamOffset = this.aimCamOffset;
		}
		else
		{
			this.targetPivotOffset = this.pivotOffset;
			this.targetCamOffset = this.camOffset;
		}
		if (this.playerControl.isSprinting())
		{
			this.targetFOV = this.sprintFOV;
		}
		else
		{
			this.targetFOV = this.defaultFOV;
		}
		this.cam.GetComponent<Camera>().fieldOfView = Mathf.Lerp(this.cam.GetComponent<Camera>().fieldOfView, this.targetFOV, Time.deltaTime);
		Vector3 a = this.player.position + rotation2 * this.targetPivotOffset;
		Vector3 point = this.targetCamOffset;
		for (float num = this.targetCamOffset.z; num <= 0f; num += 0.5f)
		{
			point.z = num;
			if (this.DoubleViewingPosCheck(a + rotation * point) || num == 0f)
			{
				this.targetCamOffset.z = point.z;
				break;
			}
		}
		if (this.playerControl.IsFlying())
		{
			this.targetCamOffset.y = 0f;
		}
		this.smoothPivotOffset = Vector3.Lerp(this.smoothPivotOffset, this.targetPivotOffset, this.smooth * Time.deltaTime);
		this.smoothCamOffset = Vector3.Lerp(this.smoothCamOffset, this.targetCamOffset, this.smooth * Time.deltaTime);
		this.cam.position = this.player.position + rotation2 * this.smoothPivotOffset + rotation * this.smoothCamOffset;
	}

	// Token: 0x06001119 RID: 4377 RVA: 0x0007E75C File Offset: 0x0007C95C
	private bool DoubleViewingPosCheck(Vector3 checkPos)
	{
		return this.ViewingPosCheck(checkPos) && this.ReverseViewingPosCheck(checkPos);
	}

	// Token: 0x0600111A RID: 4378 RVA: 0x0007E774 File Offset: 0x0007C974
	private bool ViewingPosCheck(Vector3 checkPos)
	{
		RaycastHit raycastHit;
		if (Physics.Raycast(checkPos, this.player.position - checkPos, out raycastHit, this.relCameraPosMag))
		{
			if (raycastHit.transform != this.player && !raycastHit.transform.GetComponent<Collider>().isTrigger)
			{
				Debug.DrawRay(this.player.position, checkPos - this.player.position, Color.blue);
				return false;
			}
			Debug.DrawRay(this.player.position, checkPos - this.player.position, Color.green);
		}
		return true;
	}

	// Token: 0x0600111B RID: 4379 RVA: 0x0007E824 File Offset: 0x0007CA24
	private bool ReverseViewingPosCheck(Vector3 checkPos)
	{
		RaycastHit raycastHit;
		if (Physics.Raycast(this.player.position, checkPos - this.player.position, out raycastHit, this.relCameraPosMag))
		{
			if (raycastHit.transform != base.transform && !raycastHit.transform.GetComponent<Collider>().isTrigger)
			{
				Debug.DrawRay(this.player.position, checkPos - this.player.position, Color.red);
				return false;
			}
			Debug.DrawRay(this.player.position, checkPos - this.player.position, Color.green);
		}
		return true;
	}

	// Token: 0x0600111C RID: 4380 RVA: 0x0007E8DC File Offset: 0x0007CADC
	private void OnGUI()
	{
		float num = Mathf.Abs((this.aimPivotOffset - this.smoothPivotOffset).magnitude);
		if (this.playerControl.IsAiming() && num < 0.05f)
		{
			GUI.DrawTexture(new Rect((float)(Screen.width / 2) - (float)this.crosshair.width * 0.5f, (float)(Screen.height / 2) - (float)this.crosshair.height * 0.5f, (float)this.crosshair.width, (float)this.crosshair.height), this.crosshair);
		}
	}

	// Token: 0x04001676 RID: 5750
	public Transform player;

	// Token: 0x04001677 RID: 5751
	public Texture2D crosshair;

	// Token: 0x04001678 RID: 5752
	public Vector3 pivotOffset = new Vector3(0f, 1f, 0f);

	// Token: 0x04001679 RID: 5753
	public Vector3 camOffset = new Vector3(0f, 0.7f, -3f);

	// Token: 0x0400167A RID: 5754
	public float smooth = 10f;

	// Token: 0x0400167B RID: 5755
	public Vector3 aimPivotOffset = new Vector3(0f, 1.7f, -0.3f);

	// Token: 0x0400167C RID: 5756
	public Vector3 aimCamOffset = new Vector3(0.8f, 0f, -1f);

	// Token: 0x0400167D RID: 5757
	public float horizontalAimingSpeed = 400f;

	// Token: 0x0400167E RID: 5758
	public float verticalAimingSpeed = 400f;

	// Token: 0x0400167F RID: 5759
	public float maxVerticalAngle = 30f;

	// Token: 0x04001680 RID: 5760
	public float flyMaxVerticalAngle = 60f;

	// Token: 0x04001681 RID: 5761
	public float minVerticalAngle = -60f;

	// Token: 0x04001682 RID: 5762
	public float mouseSensitivity = 0.3f;

	// Token: 0x04001683 RID: 5763
	public float sprintFOV = 100f;

	// Token: 0x04001684 RID: 5764
	private Player playerControl;

	// Token: 0x04001685 RID: 5765
	private float angleH;

	// Token: 0x04001686 RID: 5766
	private float angleV;

	// Token: 0x04001687 RID: 5767
	private Transform cam;

	// Token: 0x04001688 RID: 5768
	private Vector3 relCameraPos;

	// Token: 0x04001689 RID: 5769
	private float relCameraPosMag;

	// Token: 0x0400168A RID: 5770
	private Vector3 smoothPivotOffset;

	// Token: 0x0400168B RID: 5771
	private Vector3 smoothCamOffset;

	// Token: 0x0400168C RID: 5772
	private Vector3 targetPivotOffset;

	// Token: 0x0400168D RID: 5773
	private Vector3 targetCamOffset;

	// Token: 0x0400168E RID: 5774
	private float defaultFOV;

	// Token: 0x0400168F RID: 5775
	private float targetFOV;
}
